import 'package:boss/color_constant.dart';
import 'package:boss/main.dart';
import 'package:boss/textStyle/text_style.dart';

import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:router/annotation.dart';
import 'package:flustars/flustars.dart' as flustars;

@RouterConfig(InterestReportFormDetailsPage.pageName)
class InterestReportFormDetailsPage extends StatefulWidget {
  static const pageName = '/interestReportFormDetailsPage';

  @Autowired()
  final DateTime startTime;

  @Autowired()
  final DateTime endTime;

  @Autowired()
  final int userId;

  const InterestReportFormDetailsPage({Key key, this.startTime, this.endTime, this.userId}) : super(key: key);

  @override
  _InterestFormStatePage createState() => _InterestFormStatePage("提成明细");
}

class _InterestFormStatePage extends BaseState<InterestReportFormDetailsPage> {
  _InterestFormStatePage(String pageName) : super(pageName);

  List acceptData = [];

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 0), () async {
      obtainDetails();
    });
  }

  //获取报表item详情
  obtainDetails() async {
    try {
      showLoadingToast();

      var res = await api.statUserPercentageDetailData(
        startTime: flustars.DateUtil.formatDate(widget.startTime),
        endTime: flustars.DateUtil.formatDate(widget.endTime),
        userId: widget.userId,
      );
      if (res.successful()) {
        acceptData = res.data ?? [];
        setState(() {});
      } else {
        toast(res.msg);
      }
    } catch (e) {} finally {
      hideLoadingToast();
    }
  }

  Widget dataItem(dynamic data) {
    return Container(
      padding: EdgeInsets.symmetric(
        horizontal: 20.w,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(color: Color.fromRGBO(0, 10, 27, .05), blurRadius: 10, offset: Offset(2, 3), spreadRadius: 3.0),
        ],
        borderRadius: BorderRadius.circular(
          16.w,
        ),
      ),
      child: Column(
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(
              width: 1.px,
              color: ColorConstant.e6e6,
            ))),
            padding: EdgeInsets.symmetric(
              vertical: 24.w,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  "${data["unionOrderId"] ?? '--'}",
                  style: TextStyleConstant().normal_1_26,
                ),
                Text(
                  "${data["foodName"] ?? '--'}",
                  style: TextStyleConstant().normal_1_28,
                ),
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(
              vertical: 22.w,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Column(
                  children: <Widget>[
                    Text(
                      "提成方式",
                      style: TextStyleConstant().normal_2_26,
                    ),
                    SizedBox(
                      height: 12.w,
                    ),
                    Text("${commissionMode(data['commissionWay'])}"),
                  ],
                ),
                Column(
                  children: <Widget>[
                    Text(
                      "提成比例",
                      style: TextStyleConstant().normal_2_26,
                    ),
                    SizedBox(
                      height: 12.w,
                    ),
                    Text("${commissionProportion(data['commissionWay'], data['commissionAmount'])}"),
                  ],
                ),
                Column(
                  children: <Widget>[
                    Text(
                      "（原）提成金额",
                      style: TextStyleConstant().normal_2_26,
                    ),
                    SizedBox(
                      height: 12.w,
                    ),
                    Text("¥${AmountUtil.price2String((data["amount"]) ?? 0)}"),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget dataList() {
    return acceptData.length == 0
        ? Center(
            child: Container(
              child: Text(
                "暂无数据",
                style: TextStyleConstant().normal_3_28,
              ),
            ),
          )
        : SingleChildScrollView(
            child: Column(
              children: (acceptData ?? [])
                  .map((str) => Container(
                        padding: EdgeInsets.symmetric(
                          vertical: 16.w,
                          horizontal: 20.w,
                        ),
                        child: dataItem(str),
                      ))
                  .toList(),
            ),
          );
  }

  //提成方式
  commissionMode(String commissionWay) {
    if (commissionWay == 'oriPricePercent') {
      return '原价百分比';
    }
    if (commissionWay == 'salePricePercent') {
      return '售价百分比';
    }
    if (commissionWay == 'fixed') {
      return '固定金额';
    }
    if (commissionWay == 'tableNum') {
      return '预订';
    }
    return '--';
  }

  //提成比例 对应的枚举 百分比 万分之一 除100+'%' 否则 为钱数
  commissionProportion(String commissionWay, int commissionAmount) {
    if (commissionWay == 'oriPricePercent' || commissionWay == 'salePricePercent') {
      return (commissionAmount / 100).toStringAsFixed(2) + '%';
    } else {
      return '¥${AmountUtil.price2String(commissionAmount ?? 0)}';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '提成明细',
      ),
      body: Container(child: dataList()),
    );
  }
}
